<template>
  <div class="examuser_box">
    <el-table :data="tableData" style="width: 100%" stripe>
      <el-table-column
        label="序号"
        type="index"
        :index="tableIndex"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        label="用户名"
        prop="username"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="昵称" prop="nickname" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="邮箱" prop="email" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="身份证明" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-image
            :src="scope.row.certificate"
            class="certificate"
            :preview-src-list="[scope.row.certificate]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button type="primary" @click="permit(scope.row)"
            >通过审核</el-button
          >
          <el-button type="danger" @click="refuse(scope.row)"
            >驳回审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="prev, pager, next"
      background
      hide-on-single-page
      class="pagination"
      :page-size="size"
      :current-page="current"
      :total="total"
      @prev-click="getTableData"
      @next-click="getTableData"
      @current-change="getTableData"
    >
    </el-pagination>
  </div>
</template>

<script>
import { Loading } from "element-ui";
import axios from "@/request";

export default {
  name: "ExamUser",
  data() {
    return {
      tableData: [],
      current: 1,
      size: 5,
      total: 100,
    };
  },
  mounted() {
    this.getTableData(1);
  },
  methods: {
    tableIndex(index) {
      return (this.current - 1) * this.size + index + 1;
    },
    getTableData(current) {
      let loading = Loading.service({
        text: "正在加载...",
        target: ".examuser_box",
      });
      axios
        .get("/auth/getNewUserList", {
          params: {
            current: current,
            size: this.size,
          },
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.total = res.data.data.total;
            this.tableData = res.data.data.records;
          } else {
            this.$message.error(res.data.data);
          }
          loading.close();
        })
        .catch((e) => {});
    },
    permit(row) {
      let loading = Loading.service({
        text: "正在加载...",
        target: ".examuser_box",
      });
      axios
        .get("/auth/permit", {
          params: {
            id: row.id,
          },
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.$message.success(res.data.data);
            if (this.current > 0 && row.index == 0) {
              this.getTableData(this.current - 1);
            } else {
              this.getTableData(this.current);
            }
          } else {
            this.$message.error(res.data.data);
          }
          loading.close();
        })
        .catch((e) => {
          loading.close();
        });
    },
    refuse(row) {
      let loading = Loading.service({
        text: "正在加载...",
        target: ".examuser_box",
      });
      axios
        .get("/auth/refuse", {
          params: {
            id: row.id,
          },
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.$message.success(res.data.data);
            if (this.current > 0 && row.index == 0) {
              this.getTableData(this.current - 1);
            } else {
              this.getTableData(this.current);
            }
          } else {
            this.$message.error(res.data.data);
          }
          loading.close();
        })
        .catch((e) => {
          loading.close();
        });
    },
  },
};
</script>

<style scoped>
.certificate >>> .el-image__inner {
  max-height: 100px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>